
'use strict';
goog.provide('Blockly.JavaScript.Inout');

goog.require('Blockly.JavaScript');



Blockly.JavaScript.inout_highlow = function() {
  var dropdown_d_value = this.getFieldValue('text_values');
  // TODO: Assemble JavaScript into code variable.
  var code = dropdown_d_value;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.JavaScript.ORDER_ATOMIC];
};




Blockly.JavaScript.inout_digital_pin = function() {
  var dropdown_d_value = this.getFieldValue('d_pin_values');
  // TODO: Assemble JavaScript into code variable.
  var code = dropdown_d_value;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.JavaScript.ORDER_ATOMIC];
};


Blockly.JavaScript.inout_analog_pin = function() {
  var dropdown_d_value = this.getFieldValue('a_pin_values');
  // TODO: Assemble JavaScript into code variable.
  var code = dropdown_d_value;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.JavaScript.ORDER_ATOMIC];
};

Blockly.JavaScript['inout_digital_write'] = function() {
  var value_pin = Blockly.JavaScript.valueToCode(this, 'pin', Blockly.JavaScript.ORDER_ATOMIC);
  var value_p_value = Blockly.JavaScript.valueToCode(this, 'p_value', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var value=1;
  if(value_p_value=='LOW'){
    value=0;
  }
  var fun_name='digital_write_'+Math.round(Math.pow(Math.random(),3)*100000000);

  var set_pin_mode='ActionBlock.exe_bock_not_reply({"method": "set_pin_mode", "params": ["'+value_pin+'", "1"]});\n';
  var pin_write='function '+fun_name+ '() {\nActionBlock.exe_bock_has_reply({"method": "digital_write", "params": ["'+value_pin+'", '+value+']},0);\n' +
    'ActionBlock.set_panel_write_data("'+value_pin+'",'+value+',0);\n}';
  Blockly.JavaScript.definitions_['set_pin_mode_'+value_pin]=set_pin_mode;
  Blockly.JavaScript.definitions_[fun_name]=pin_write;

  var code = fun_name+'();\n';
  return code;
};


Blockly.JavaScript['inout_digital_read'] = function() {
  var value_pin = Blockly.JavaScript.valueToCode(this, 'pin', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.

  var fun_name='digital_read_'+Math.round(Math.pow(Math.random(),3)*100000000);

  var set_pin_mode='ActionBlock.exe_bock_not_reply({"method": "set_pin_mode", "params": ["'+value_pin+'", "0"]});\n';
  var pin_write='function '+fun_name+ '() {\nActionBlock.exe_bock_has_reply({"method": "digital_read", "params": ["'+value_pin+'"]},0);\n' +
    'ActionBlock.set_panel_read_data("'+value_pin+'",0);\nreturn ActionBlock.reply_data;\n}';
  Blockly.JavaScript.definitions_['set_pin_mode_'+value_pin]=set_pin_mode;
  Blockly.JavaScript.definitions_[fun_name]=pin_write;

  var code = fun_name+'()';

  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.JavaScript.ORDER_NONE];
};

Blockly.JavaScript['inout_analog_write'] = function() {
  var value_pin = Blockly.JavaScript.valueToCode(this, 'pin', Blockly.JavaScript.ORDER_ATOMIC);
  var value_p_value = Blockly.JavaScript.valueToCode(this, 'p_value', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var new_pin=null;
  var pin_mode=null;
  switch (value_pin){
    case "A0":
      new_pin='14';
      pin_mode="1";
      break;
    case "A1":
      new_pin="15";
      pin_mode="1";
      break;
    case "A2":
      new_pin="16";
      pin_mode="1";
      break;
    case "A3":
      new_pin="17";
      pin_mode="1";
      break;
    case "A4":
      new_pin="18";
      pin_mode="1";
      break;
    case "A5":
      new_pin="19";
      pin_mode="1";
      break;
    default:
      new_pin=value_pin;
      pin_mode="3";
      break;
  }

  var fun_name='analog_write_'+Math.round(Math.pow(Math.random(),3)*100000000);

  var set_pin_mode='ActionBlock.exe_bock_not_reply({"method": "set_pin_mode", "params": ["'+new_pin+'", "'+pin_mode+'"]});\n';
  var pin_write='function '+fun_name+ '() {\nvar res='+value_p_value+';ActionBlock.exe_bock_has_reply({"method": "analog_write", "params": ["'+new_pin+'", res]},0);\n' +
    'ActionBlock.set_panel_write_data("'+value_pin+'",res,0);\n}';
  Blockly.JavaScript.definitions_['set_pin_mode_'+new_pin]=set_pin_mode;
  Blockly.JavaScript.definitions_[fun_name]=pin_write;

  var code = fun_name+'();\n';
  return code;
};


Blockly.JavaScript['inout_analog_read'] = function() {
  var value_pin = Blockly.JavaScript.valueToCode(this, 'pin', Blockly.JavaScript.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var new_pin=null;
  var pin_mode=null;
  switch (value_pin){
    case "A0":
      new_pin='0';
      pin_mode="2";
      break;
    case "A1":
      new_pin="1";
      pin_mode="2";
      break;
    case "A2":
      new_pin="2";
      pin_mode="2";
      break;
    case "A3":
      new_pin="3";
      pin_mode="2";
      break;
    case "A4":
      new_pin="4";
      pin_mode="2";
      break;
    case "A5":
      new_pin="5";
      pin_mode="2";
      break;
    default:
      new_pin=value_pin;
      pin_mode="2";
      break;
  }

  var fun_name='analog_read_'+Math.round(Math.pow(Math.random(),3)*100000000);

  var set_pin_mode='ActionBlock.exe_bock_not_reply({"method": "set_pin_mode", "params": ["'+new_pin+'", "'+pin_mode+'"]});\n';
  var pin_write='function '+fun_name+ '() {\nvar res=ActionBlock.exe_bock_has_reply({"method": "analog_read", "params": ["'+new_pin+'"]},0);\n' +
    'ActionBlock.set_panel_read_data("'+value_pin+'",res,0);\nreturn res;\n}';
  Blockly.JavaScript.definitions_['set_pin_mode_'+new_pin]=set_pin_mode;
  Blockly.JavaScript.definitions_[fun_name]=pin_write;

  var code = fun_name+'()';
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.JavaScript.ORDER_NONE];
};
